package gwtmodule1.client.gui.shared;

import gwtmodule1.client.gui.utils.Currency;
import gwtmodule1.client.gui.utils.HSpacer;
import gwtmodule1.client.gui.utils.Table;

import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class OrderSummary extends VerticalPanel {

	private class ProductList extends Table {

		private float total = 0.0f;

		public ProductList() {
			super();
		}

		public void addItem(int quantity, String company, String product,
				float price, float taxpercent) {
			AddRow r = addRow();
			r.add(quantity + "x " + company + " " + product + ": "
					+ Currency.format(price));
			float tax = quantity * price * (taxpercent / 100f);
			r.add("+ " + Currency.format(tax) + " tax (" + taxpercent + "%)")
					.setHAlign(r.RIGHT);
			float tot = (quantity * price) + tax;
			r.add("= " + Currency.format(tot)).setHAlign(r.RIGHT);
			total += tot;
		}

		public void addShipping(float price) {
			AddRow r = addRow();
			r.add("Shipping:").setColspan(2).setHAlign(r.RIGHT);
			r.add(new Currency(price));
			total += price;
		}

		public void addTotal() {
			AddRow r = addRow();
			r.add("Total:").setColspan(2).setHAlign(r.RIGHT);
			r.add(new Currency(total)).bold();
		}

	}

	public OrderSummary() {
		super();
		add(new HTML("OrderID: <b>1234</b>"));
		add(new HTML("Date: 29/02/06 5:35pm"));
		add(new HTML("Customer: Fritz Mueller"));
		add(new HTML("Payment: Credit Card (Visa Card)"));
		add(new HSpacer());
		add(new HTML("<b>Selected Products</b>"));
		add(createProductList());
		add(new HSpacer());
		add(new HTML("<b>Shipping and Payment Information</b>"));
		add(createDeleveryAndPaymentInfo());
		add(new HSpacer());
		add(createStoreInfo());
	}

	private Widget createDeleveryAndPaymentInfo() {
		VerticalPanel panel = new VerticalPanel();
		panel.add(new HTML(
				"Shipping Address: 16th Street 3456, La Crosse, Wi 45345"));
		panel.add(new HTML(
				"Billing Address: 16th Street 3456, La Crosse, Wi 45345"));
		return panel;
	}

	private Widget createProductList() {
		ProductList pl = new ProductList();

		for (int i = 0; i < 6; i++) {
			int c = (i * 3 % 4) + 1;
			pl.addItem(c, "Company" + i, "Product" + i, ((i + 2) * 10), 5.5f);
		}
		pl.addItem(1, "QAY", "Gift Card", 50, 0);
		pl.addShipping(12.5f);
		pl.addTotal();
		return pl;
	}

	private Widget createStoreInfo() {
		VerticalPanel panel = new VerticalPanel();
		panel.add(new HTML("<b>QAY Online Store</b>"));
		panel.add(new HTML("17th Street 124, La Crosse, Wi 45345"));
		return panel;
	}

}
